<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_BuyInvoiceController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        parent::loadTemplate('alpona');
        $this->view->headTitle = "create buy invoice";
	}
	
	public function indexAction(){
		$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$model = new Admin_Model_Supplier();
        $data = $model->getDatas();
        
        $this->view->action = 'create';
        $this->view->data = $data;
	}
    
    public function createAction(){
        parent::addScripts(array('jquery-price-format.min.js', 'autoNumeric.js', 'jquery-ui-1.10.3.custom.min.js', 'jquery.datetimepicker.js'));
        parent::addStyleSheet(array('jquery-ui-1.10.3.custom.css', 'jquery.datetimepicker.css'));
        $request = $this->_request;
        
        if($request->isPost()){
            $supplier_id = $request->getParam('supplier_id');
            
            $supplier_model = new Admin_Model_Supplier();
            $suppliers = $supplier_model->getDatas();
            
            $brand_model = new Admin_Model_Brand();
            $brands = $brand_model->getDatas();
            
            $this->view->brands = $brands;
            $this->view->suppliers = $suppliers;
            
            $model = new Admin_Model_ProductBuy();
            $products = $model->getDatas($supplier_id);
            
            $supplier = $supplier_model->getData($supplier_id);
            
            $model_buy_invoice = new Admin_Model_BuyInvoice();
            
            $model_liabilities = new Admin_Model_BuyLiabilities();
            $dept = $model_liabilities->getDept($supplier_id);
            $payment = $model_liabilities->getPayment($supplier_id);
            $order = $model_liabilities->getOrders($supplier_id);
            $refund = $model_liabilities->getRefund($supplier_id);
            
            $this->view->refund = $refund;
            $this->view->order = $order;
            $this->view->dept = $dept;
            $this->view->payment = $payment;
            $this->view->products = $products;
            $this->view->supplier_id = $supplier_id;
            $this->view->supplier = $supplier;
            $this->view->totalDept = $this->getDept($supplier_id);
            
            $save = $request->getParam('save', false);
            
            if($save){
                $validator = new Admin_Form_ValidateBuyInvoice($request->getParams());
                $data = $validator->getData();
                
                $model_buy_invoice->updateOrders($data['subtotal'], $data['date'], $data['supplier_id']);
                $orderID = $model_buy_invoice->saveData($data);
                
                $this->view->orderID = $orderID;
                //$this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'manage'), null, true);
            }
        }
    }
    
    public function editAction(){
        parent::addScripts(array('jquery-price-format.min.js', 'autoNumeric.js', 'jquery.datetimepicker.js', 'jquery-ui-1.10.3.custom.min.js'));
        parent::addStyleSheet(array('jquery-ui-1.10.3.custom.css', 'jquery.datetimepicker.css'));
    	$request = $this->_request;
        
        $supplier_id = $request->getParam('supplier_id');
        $order_id = $request->getParam('id');
        
        $model = new Admin_Model_ProductBuy();
        $products = $model->getDatas($supplier_id);
        
        $supplier_model = new Admin_Model_Supplier();
        $supplier = $supplier_model->getData($supplier_id);
        
        $brand_model = new Admin_Model_Brand();
        $brands = $brand_model->getDatas();
        
        $this->view->brands = $brands;
        
        $model_buy_invoice = new Admin_Model_BuyInvoice();
        
        $model_liabilities = new Admin_Model_BuyLiabilities();
        $old_order = $model_buy_invoice->getData($order_id);
        $date = $old_order['order']['date'];
        $dept = $model_liabilities->getDept($supplier_id, $date);
        $payment = $model_liabilities->getPayment($supplier_id, $date);
        $order = $model_liabilities->getOrders($supplier_id, $date);
        $refund = $model_liabilities->getRefund($supplier_id, $date);
        
        $this->view->refund = $refund;
        $this->view->order = $order;
        $this->view->old_order = $old_order;
        $this->view->dept = $dept;
        $this->view->payment = $payment;
        $this->view->products = $products;
        $this->view->supplier_id = $supplier_id;
        $this->view->supplier = $supplier;
        $this->view->totalDept = $this->getDept($supplier_id, $date);
        
        $save = $request->getParam('save', false);
        
        if($save){
            $validator = new Admin_Form_ValidateBuyInvoice($request->getParams(), true, $old_order);
            $data = $validator->getData();
            $data['order_id'] = $old_order['order']['id'];
            $remaining = $data['remaining'] - $old_order['order']['remaining'];
            
            $model_buy_invoice->updateOrders($remaining, $data['date'], $old_order['order']['supplier_id']);
            $orderID = $model_buy_invoice->saveData($data);
            
            $this->view->orderID = $orderID;
            //$this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'manage'), null, true);
        }
    }
    
    protected function getDept($supplier_id, $date = null){
        $totalDept = 0;
        
        $model_liabilities = new Admin_Model_BuyLiabilities();
        
        $dept = $model_liabilities->getDept($supplier_id, $date);
        $payment = $model_liabilities->getPayment($supplier_id, $date);
        $order = $model_liabilities->getOrders($supplier_id, $date);
        $refund = $model_liabilities->getRefund($supplier_id, $date);
        
        $totalDept = $dept->dept + $order->subtotal - $order->totalpay - $payment->total - $refund->total;
        
        return $totalDept;
    }
    
    public function getDeptAjaxAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $supplier_id = $request->getParam('supplier_id', false);
            $date = $request->getParam('date', false);
            
            $dateNow = DateTime::createFromFormat("d/m/Y H:i", $date);
            $date = $dateNow->format("Y-m-d H:i:s");
            
            $currOrder = $request->getParam('currOrder', false);
            
            $totalDept = "false";
            
            $totalDept = $this->getDept($supplier_id, $date);
            
            if($currOrder != false){
                $model = new Admin_Model_BuyInvoice();
                $order = $model->getData($currOrder);
                
                $datetime1 = new DateTime($order['order']['date']);
                $datetime2 = new DateTime($date);
                
                if($datetime1 < $datetime2){
                    $totalDept = $totalDept - $order['order']['subtotal'];
                }
            }
            
            echo $totalDept;
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
    
    public function viewAction(){
    	$request = $this->_request;
        $orderID = $request->getParam('order_id');
        
        $model = new Admin_Model_BuyInvoice();
        
        if($model->checkExists($orderID)){
            $rs = $model->getData($orderID);
            
            $this->view->order = $rs['order'];
            $this->view->products = $rs['products'];
        }
        
        $this->_helper->layout->disableLayout();
    }
    
    public function manageAction(){
        $this->view->headTitle = "manage buy invoice";
    	$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$model = new Admin_Model_BuyInvoice();
        $data = $model->getDatas();
        
        $this->view->data = $data;
    }
    
    public function removeAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $model = new Admin_Model_BuyInvoice();
            $rs = $model->deleteData($request->getParam('order_id'));
        }
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'manage'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function viewQuantityAction(){
        $this->view->headTitle = "Các sản phẩm đã mua";
        $scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
        
        $model = new Admin_Model_BuyLiabilities();
        $data = $model->viewQuantity();
        
        $this->view->data = $data;
    }
    
    public function updatePriceAction(){
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $data = array();
            
            $data['id'] = $request->getParam('pid');
            $data['price'] = $request->getParam('price');
            
            $model = new Admin_Model_ProductBuy();
            $result = $model->saveData($data);
            
            if($result > 0)
                echo "true";
            else
                echo "false";
        }else{
            echo "false";
        }
    }
}